package com.ffmpeg.player;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.Log;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created by aoni-dingbb on 2016/9/12.
 */
public class RtspPlayerView extends GLSurfaceView implements RtspPlayer.OnFrameReadyListener {
    private  OnSurfaceViewListener surfaceViewListener = null;
    public interface OnSurfaceViewListener {
        void onRtspPlayerViewSurfaceCreated();
        void onRtspPlayerViewSurfaceChanged();
    }

    public void setRtspPlayerSurfaceListerner(OnSurfaceViewListener listerner) {
        surfaceViewListener = listerner;
    }

    public RtspPlayerView(Context context) {
        super(context);
        setEGLContextClientVersion(2);
        setRenderer(new CustomRenderer());
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        RtspPlayer.getInstance().setFrameReadyListener(this);
    }

    public void setView(int x, int y, int w, int h) {
        RtspPlayer.getInstance().setViewLayoutParam(x, y, w, h);
    }

    @Override
    public void onFrameReady() {
        this.requestRender();
    }

    public class CustomRenderer implements Renderer {
        public void onSurfaceCreated(GL10 gl10, EGLConfig config) {
            //Do nothing
            RtspPlayer.getInstance().init();
            if (surfaceViewListener != null) {
                surfaceViewListener.onRtspPlayerViewSurfaceCreated();
            }
        }

        public void onDrawFrame(GL10 gl10) {
            RtspPlayer.getInstance().render();
        }

        public void onSurfaceChanged(GL10 gl10, int width, int height) {
            RtspPlayer.getInstance().setViewLayoutParam(0, 0, width, height);
            if (surfaceViewListener != null) {
                surfaceViewListener.onRtspPlayerViewSurfaceChanged();
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}
